function hideTextFieldLabel(id) {
	var textfield = document.getElementById(id);
	var label = document.getElementById(id + "-label");
	
	textfield.style.opacity = 1;
	label.style.display = "none";
}

function showTextFieldLabel(id) {
	var textfield = document.getElementById(id);
	var label = document.getElementById(id + "-label");
	
	if (textfield.value != "") {
		textfield.style.opacity = 1;
		label.style.display = "none";
	} else {
		textfield.style.opacity = 0;
		label.style.display = "block";
	}
}

function freeTextSearch(evt) {
	evt = (evt) ? evt : event;
	
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (charCode == 13 || charCode == 3) {
		var textbox = document.getElementById("search-text-box");
		var text = textbox.value;
		
		window.location = 
			urlWithoutPaging +
			'&-=@lwq.project.TextIndexProjector;;@lwq.bucket.TextIndexBucketer;' + text;
	}
}

function loadView(pseudoURI, profileID, prefix, suffix, properties) {
	var s = "";
	if (properties) {
		for (var i = 0; i < properties.length; i++) {
			var pair = properties[i];
			s = s + pair.name + "=" + pair.value + "\n";
		}
	}
	s += "outerQuery=" + g_outerQuery + "\n";
	
	if (!prefix) {
		prefix = "";
	}
	if (!suffix) {
		suffix = "";
	}
		
	HTTPUtilities.doPost(
		g_contextPath + "/" + profileID + "?command=view&pseudoURI=" + encodeURIComponent(pseudoURI),
		s,
		function(status, statusText) {
			//alert("Failed to load view for item with URI = " + pseudoURI);
		},
		function (text) {
			var elmt = document.getElementById(prefix + pseudoURI + suffix);
			elmt.innerHTML = text;
		}
	);
}


function loadViewForPrint(pseudoURI, profileID, prefix, suffix, properties) {
	var s = "";
	if (properties) {
		for (var i = 0; i < properties.length; i++) {
			var pair = properties[i];
			s = s + pair.name + "=" + pair.value + "\n";
		}
	}
	s += "outerQuery=" + g_outerQuery + "\n";
	
	if (!prefix) {
		prefix = "";
	}
	if (!suffix) {
		suffix = "";
	}
		
	HTTPUtilities.doPost(
		g_contextPath + "/" + profileID + "?command=printView&pseudoURI=" + encodeURIComponent(pseudoURI),
		s,
		function(status, statusText) {
			//alert("Failed to load view for item with URI = " + pseudoURI);
		},
		function (text) {
			var elmt = document.getElementById(prefix + pseudoURI + suffix);
			elmt.innerHTML = text;
		}
	);
}


function reloadView(pseudoURI, profileID, prefix, suffix, properties) {
	if (!properties) {
		properties = [];
	}
	properties.unshift({ name : "showRenderTime" , value : true });
	
	loadView(pseudoURI, profileID, prefix, suffix, properties);
}

function expandReferers(pseudoURI, profileID) {
	var expandDiv = document.getElementById("expand-referers-" + pseudoURI);
	var collapseDiv = document.getElementById("collapse-referers-" + pseudoURI);
	
	expandDiv.style.display = "none";
	collapseDiv.style.display = "block";

	HTTPUtilities.doPost(
		g_contextPath + "/" + profileID + "?command=view-referers&pseudoURI=" + encodeURIComponent(pseudoURI),
		"outerQuery=" + g_outerQuery + "\n",
		function(status, statusText) {
			//alert("Failed to load view for item with URI = " + pseudoURI);
		},
		function (text) {
			var elmt = document.getElementById("referers-" + pseudoURI);
			elmt.innerHTML = text;
		}
	);
}

function collapseReferers(pseudoURI, profileID) {
	var expandDiv = document.getElementById("expand-referers-" + pseudoURI);
	var collapseDiv = document.getElementById("collapse-referers-" + pseudoURI);
	
	collapseDiv.style.display = "none";
	expandDiv.style.display = "block";
}

function browseTo(url) {
	window.location = url;
}

function getSource(profileID,uri,propertyURI,value){
	var index = propertyURI.indexOf("#");
	var ontology = propertyURI.substring(0,index);
	//alert(ontology);
	var ontologyProperty = propertyURI.substring(index+1,propertyURI.length);
	//alert(ontology +" "+ontologyProperty+" "+value);
	var url = g_contextPath + "/" + profileID + "?command=getSource&uri=" + uri+
	"&ontology=" +ontology+"&ontProp="+ontologyProperty+"&value="+value;
	//alert(url);
	HTTPUtilities.doPost(url,"",
			function(status, statusText) {
				//alert("Failed to load view for item with URI = " + pseudoURI);
			},
			function (text) {
				if(text!=""){
					var lvalue = document.getElementById(uri+propertyURI).innerHTML;
					var larr = lvalue.split(' ');
					var len = 0;				
					// For each iterates over the index of arrays
					for(var i in larr) { 
					     len += larr[ i ].length; // Acummulate the length of all the strings
					}
					
					if(len==0){
						document.getElementById(uri+propertyURI).style.display = 'block';
						document.getElementById(uri+propertyURI).style.background='#FFFF66';
						document.getElementById(uri+propertyURI).innerHTML = text;
					}
					else{
						 document.getElementById(uri+propertyURI).style.display = 'none';
						 document.getElementById(uri+propertyURI).innerHTML="";
					}
				}
			}
	);
}

